Try Catch
using System;
/* Code that may throw an exception is placed inside a try block. If an exception is thrown in a try block,
* control is passed to the catch block */
public class TryCatchDemo
{
static void Main(String[] args)
{
try
{
int[] a = new int[2];
Console.WriteLine("Access element three :" + a[3]);
}
catch (IndexOutOfRangeException e)
{
//IndexOutOfRangeException is caught here
Console.WriteLine("Exception thrown :" + e);
}
Console.WriteLine("Out of the block");
Console.ReadLine();
}
}
Output:
Exception thrown :System.IndexOutOfRangeException: Index was outside the bounds of the array